package com.ld.hpfs.service.impl;


import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ld.hpfs.dao.LeftMapper;
import com.ld.hpfs.service.LeftService;
@Service
public class LeftServiceImpl implements LeftService{
	private static final Logger logger = LoggerFactory.getLogger(LeftServiceImpl.class);

	@Autowired
	private LeftMapper leftMapper;

	//    @Override
	//	public ArrayList<HashMap<String,Object>> getLeftList(HashMap<String,Object> param){
	//		return leftMapper.getLeftList( param);
	//	}

	//将菜单信息添加到菜单节点中
	private Map<String, Object> createMenuNode(Map<String, Object> groupsMap, Map<String, Object> row)
	{
		HashMap<String, Object> node = new HashMap<String, Object>();
		node.put("panel",row.get("EXT_PANEL"));
		node.put("id", row.get("EXT_ID"));
		node.put("children", new ArrayList<Object>());
		node.put("text", row.get("MENU_NAME"));	        	        
		node.put("expanded", true);
		node.put("groups", groupsMap.get(row.get("MENU_HID").toString()) == null ? new ArrayList<HashMap<String, Object>>() : groupsMap.get(row.get("MENU_HID").toString()));//获得对应menu_id中的groups
		node.put("iconCls", row.get("EXT_ICON"));
		if(row.get("EXT_PANEL").equals("")){
			node.put("leaf", false);  
		}else{
			node.put("leaf", true); 
		}
		return node;
	}
	
	//  将每行的function追加到 groups数组中,形成button组
	private void appendFunc(Map<String, Object> content, Map<String, Object> row)
	{			
		ArrayList<HashMap<String, Object>> menuAry = (ArrayList<HashMap<String, Object>>) content.get(row.get("MENU_ID").toString());//groups
		if(menuAry == null)
		{
			menuAry = new ArrayList<HashMap<String, Object>>();
			content.put(row.get("MENU_ID").toString(), menuAry);
		}
		Map<String, Object> groupsNode = null;
		if(menuAry.size() > 0)
		{
			for(HashMap<String, Object> menu : menuAry){
			  if(menu.get("group").equals(row.get("EXT_GROUP")))
			  {
				groupsNode = menu;
			  }
			}
		}
		
		if(groupsNode == null)
		{
			groupsNode = new HashMap<String, Object>();
			groupsNode.put("group", row.get("EXT_GROUP"));
			groupsNode.put("buttons", new ArrayList<Object>());
			menuAry.add((HashMap<String, Object>)groupsNode);
		}
		
		HashMap<String, Object> tempbuttons = new HashMap<String, Object>();
		tempbuttons.put("id", row.get("EXT_ID"));
		tempbuttons.put("scale","large");
		tempbuttons.put("text", row.get("FUNCTION_NAME"));
		tempbuttons.put("iconAlign","top");
		tempbuttons.put("width",80);
		tempbuttons.put("iconCls",row.get("EXT_ICON"));
		
		ArrayList<Object> buttonsNode = (ArrayList<Object>)groupsNode.get("buttons");
		buttonsNode.add(tempbuttons);
	}

	//将每项菜单追加到children中
	private void appendMenu(Map<String, Object> centerMap, Map<String, Object> parentMap, Map<String, Object> groupsMap, Map<String, Object> row,ArrayList<HashMap<String,Object>> result)
	{
		Map<String, Object> parentNode = null;
		if((Integer)(row.get("PARENT_ID")) == 0)
		{
			// 1. 处理根节点，首先检索中心是否存在，不存在则新建，之后追加根节点
			parentNode = (Map<String, Object>)centerMap.get(row.get("CENTER_NAME"));
			if(parentNode == null)
			{
				parentNode = new HashMap<String, Object>();
				parentNode.put("centerName", row.get("CENTER_NAME"));
				parentNode.put("children", new ArrayList<Object>());	
				centerMap.put(row.get("CENTER_NAME").toString(), parentNode);
				result.add((HashMap<String,Object>)parentNode);
			}
		}
		else
		{
			parentNode = (Map<String, Object>)parentMap.get(row.get("PARENT_ID").toString());
//			if(parentNode == null)
//			{
//				return false;
//			}
		}
		
		List<Object> menuNodes = (List<Object>)parentNode.get("children");
		
		Map<String, Object> menuNode = createMenuNode(groupsMap, row);
		menuNodes.add(menuNode);
		
		parentMap.put(row.get("MENU_HID").toString(), menuNode);
	}

	private void cleanMenu(Map<String, Object> centerMap)
	{
		// List<Hash<String, Object>> children
	}

	@Override
	public ArrayList<HashMap<String,Object>> getLeftList(HashMap<String,Object> param){	
		ArrayList<HashMap<String,Object>> result1 = new ArrayList<HashMap<String,Object>>();
		if(param.get("lang").equals("en")){
		  result1 =  leftMapper.getLeftFunctionList2( param);
		}else{
	      result1 =  leftMapper.getLeftFunctionList1( param);	
		}//生成groups,key:menu_id,value:[buttons（数组）、group]数组
		Map<String,Object> menuFuncMap = new HashMap<String,Object>();
		for(HashMap<String, Object> row : result1)
		{
			appendFunc(menuFuncMap,row);//将每行的function追加到 groups数组中			    
		}
        
		ArrayList<HashMap<String,Object>> result2 = new ArrayList<HashMap<String,Object>>();
		if(param.get("lang").equals("en")){
			result2 =  leftMapper.getLeftMenuList2( param);
		}else{
			result2 =  leftMapper.getLeftMenuList1( param);	
			}//生成menu树，key:center_name,value:centerName,children数组
		Map<String, Object> centerMap = new HashMap<String, Object>();//6个中心
		Map<String, Object> parentMap = new HashMap<String, Object>();//将每个row并列放入对应centername中
		ArrayList<HashMap<String,Object>> result = new ArrayList<HashMap<String,Object>>();
		for (HashMap<String,Object> row : result2)
		{
			appendMenu(centerMap,parentMap, menuFuncMap, row,result);		
		}	
		
		return result;
	}	
	
	@Override
	public ArrayList<HashMap<String,Object>> getLineStoreList(HashMap<String,Object> param){
		return leftMapper.getLineStoreList(param);
	}
	
	@Override
	public ArrayList<HashMap<String,Object>> getDocTypeList(HashMap<String,Object> param){
		return leftMapper.getDocTypeList(param);
	}
	
	@Override
	public ArrayList<HashMap<String,Object>> getColumnStoreList(HashMap<String,Object> param){
		return leftMapper.getColumnStoreList(param);
	}
	
	@Override
	public ArrayList<HashMap<String,Object>> getRedDevilList(HashMap<String,Object> param){
		return leftMapper.getRedDevilList(param);
	}
	
	@Override
	public ArrayList<HashMap<String,Object>> getColumnStoreCount(HashMap<String,Object> param){
		return leftMapper.getColumnStoreCount(param);
	}
	
	@Override
	public ArrayList<HashMap<String,Object>> getPieStoreList(HashMap<String,Object> param){
		ArrayList<HashMap<String, Object>> tempArr = leftMapper.getPieStoreList(param);
		Object name[] = {"导入成功",
				         "导入失败",
				         "生成凭证成功",
				         "生成凭证失败",
				         "过账成功",
				         "过账失败"
		};
		Object name1[] = {"Import Success",
		                  "Import Fail",
		                  "Gen Voucher Success",
		                  "Gen Voucher Fail",
		                  "Post Success",
		                  "Post Fail"
       };
		ArrayList<HashMap<String,Object>> resultArr = new ArrayList<HashMap<String,Object>>();
		if(param.get("lang").equals("en")){//国际化
		   for(int i = 0 ; i < tempArr.size() ; i++ ){
			  HashMap<String,Object> result = new HashMap<String,Object>();
			  result.put("name", name1[(Integer) tempArr.get(i).get("id")]);
			  result.put("data", tempArr.get(i).get("cnt"));
			  resultArr.add(result);
		   }
		}else{
			for(int i = 0 ; i < tempArr.size() ; i++ ){
				  HashMap<String,Object> result = new HashMap<String,Object>();
				  result.put("name", name[(Integer) tempArr.get(i).get("id")]);
				  result.put("data", tempArr.get(i).get("cnt"));
				  resultArr.add(result);
			   }
		}
		return resultArr;
	}
	
	@Override
	public ArrayList<HashMap<String,Object>> getMessageBoxList(int companyId, int userId, String language){
		
		// 用于添加红废票消息
		HashMap<String, Object> param = new HashMap<String, Object>();
		param.put("companyId", companyId);
		param.put("userId", userId);
		ArrayList<HashMap<String, Object>> roles = leftMapper.getAbandonCheckerRoles(param);
		param.put("roleList", roles);
		param.put("language", language);
		return leftMapper.getMessageBoxList(param);
	}
	
	@Override
	public ArrayList<HashMap<String,Object>> getInvoiceInitConfig(HashMap<String,Object> param){
		try{
			return leftMapper.getInvoiceInitConfig(param);//获取发票设置信息
		}catch(Exception ex){
			logger.error(ex.getMessage());
			return null;
		}
	}
	
	@Override
	public int getLineMaxValue(HashMap<String,Object> param){
		return leftMapper.getLineMaxValue(param);
	}
}